/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.biggrid.plier.tools;

//import nl.uva.pcc.dnprov.test.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import nl.biggrid.plier.opm.OPMGraph;
//import nl.wtcw.vle.plier.engine.PlierEngine;
//import nl.wtcw.vle.plier.opm.OPMGraph;

/**
 *
 * @author ammar
 */
public class OPM_AMC_PlierCrawler_old {

    //static String[] workflows = new String[1000];
    // 4 simple, 4 medium, 4 complex, 1 very complex
    static String[] workflows = {"workflow-057b7f3c", "workflow-0KGsEe", "workflow-0C9lBF", "workflow-OFwTb8", "workflow-1CEKqJ", "workflow-0R5yMM", "workflow-0PESgb", "workflow-0EqX4I", "workflow-0b5b66c6", "workflow-1RROa5", "workflow-0FPh8F", "workflow-0d9a5b53", "workflow-0Ggpr5"};
    static int w = 10000;

    public static void main(String[] args) {
        // Set java security up
        boolean propModified = false;
        Properties sysProp = System.getProperties();
        String ksTKS = sysProp.getProperty("javax.net.ssl.trustStore");
        if (ksTKS == null) {
            propModified = true;
            sysProp.put("javax.net.ssl.trustStore", "ab.jks");
        }
        String ksPWD = sysProp.getProperty("javax.net.ssl.keyStorePassword");
        if (ksPWD == null) {
            propModified = true;
            sysProp.put("javax.net.ssl.keyStorePassword", "ammar99");
        }
        if (propModified) {
            System.setProperties(sysProp);
        }
        // End java security management
        
        try {

            String url = "https://hangplek.nikhef.nl/workflows//";
            System.out.print("[PLIER CRAWLER] connecting to: " + url + "... ");
            //getWorkflowIds(url);
            PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
            // Persistence of an OPMGraph.
            PersistenceManager pm = pmf.getPersistenceManager();

            System.out.println("DataNucleus AccessPlatform with JDO");
            System.out.println("===================================");


            // test case 17 and 19
            //int i = 3;
            int i = 0;
            while (i < workflows.length) {
//                if (i%20 ==0){
//                   System.out.println("\n\n --------------- I am going to sleep for 30s --------------------\n");
//                   long t0,t1;
//                    t0=System.currentTimeMillis();
//                    do{
//                        t1=System.currentTimeMillis();
//                    }
//                    while (t1-t0<30000);
//                }
                String wid = workflows[i];
                //String wid = "workflow-0R5yMM";
                System.out.println("\n[PLIER CRAWLER] Proceeding with workflow: " + i + "- " + wid);
                Transaction tx = pm.currentTransaction();
                try {
                    tx.begin();
                    System.out.println("Persisting OPM graph");
                    OPM_AMC_instance oai = new OPM_AMC_instance(wid, url);
                    OPMGraph opm = oai.getOPMGraph();
                    //opmGraph.SetInverseRelations();
                    pm.makePersistent(opm);

                    tx.commit();
                    System.out.println("OPMGraph has been persisted");
                } finally {
                    if (tx.isActive()) {
                        tx.rollback();
                    }
                }
                System.out.println("### END INSERTION ###");


                //engine.storeObject(opm);
//                System.out.print("[PLIER CRAWLER] Result XML (" + wid + ")\n");
//                System.out.print(engine.toXML(opm));
                System.out.print("\n\n\n");

                i++;

            }
                    pm.close();
        //engine.close();



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static public void getWorkflowIds(String url_str) throws Exception {
        SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        URL url = new URL(url_str);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sslsocketfactory);
        InputStream inputstream = conn.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        String string = null;
        System.out.println("Connected.");
        System.out.print("receiving workflow IDs ");
        int s = 0;
        while (((string = bufferedreader.readLine()) != null) && s < 1000) {
            if (string.indexOf("<a href=") > 0 && string.indexOf("/\">") > 0) {
                workflows[s++] = string.substring(string.indexOf("<a href=") + 9, string.indexOf("/\">"));
                System.out.print(".");
//                System.out.println("workflow " + (i++) + ": " + string.substring(string.indexOf("<a href=")+9,string.indexOf("/\">")));
            }
        }

    }
}
